package com.signway.javaui.slice;

import com.signway.javaui.ResourceTable;
import com.signway.javaui.base.BaseAbilitySlice;
import com.signway.javaui.define.CommonData;
import com.signway.javaui.utils.SwLog;
import com.signway.javaui.utils.SwToast;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;

public class ComponentAbilitySlice extends BaseAbilitySlice {
    public static final String ABILITY_NAME = CommonData.COMPONENT_ABILITY;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_component);
        initView();
    }

    private void initView() {
        /* 文本类组件 */
        Button btnText = (Button) findComponentById(ResourceTable.Id_text_component);
        /* 按钮类组件 */
        Button btnButton = (Button) findComponentById(ResourceTable.Id_button_component);
        /* 进度条类组件 */
        Button btnProgress = (Button) findComponentById(ResourceTable.Id_progress_component);
        /* 图像类组件 */
        Button btnImage = (Button) findComponentById(ResourceTable.Id_image_component);
        /* 列表容器类组件 */
        Button btnListContainer = (Button) findComponentById(ResourceTable.Id_list_container);
        /* 滑动选择器 */
        Button btnSlider = (Button) findComponentById(ResourceTable.Id_slider_component);
        /* 组件状态 */
        Button btnFlag = (Button) findComponentById(ResourceTable.Id_flag_component);
        /* 自定义组件 */
        Button btnCustom = (Button) findComponentById(ResourceTable.Id_custom_component);

        btnText.setClickedListener(component -> startAbilitySlice(CommonData.TEXT_SLICE));
        btnButton.setClickedListener(component -> startAbilitySlice(CommonData.BUTTON_SLICE));
        btnProgress.setClickedListener(component -> startAbilitySlice(CommonData.PROGRESS_SLICE));
        btnImage.setClickedListener(component -> startAbilitySlice(CommonData.IMAGE_SLICE));
        btnListContainer.setClickedListener(component -> startAbilitySlice(CommonData.LIST_CONTAINER_SLICE));
        btnSlider.setClickedListener(component -> startAbilitySlice(CommonData.PICKER_SLICE));
        btnFlag.setClickedListener(component -> startAbilitySlice(CommonData.FLAG_SLICE));
        btnCustom.setClickedListener(component -> startAbilitySlice(CommonData.CUSTOM_SLICE));
    }

    private void startAbilitySlice(String abilitySliceName) {
        if (abilitySliceName.isEmpty()) {
            SwToast.showToast(this, "开发中...");
            SwLog.info("开发中...");
            return;
        }
        SwLog.info("Start Slice is " + abilitySliceName);
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(getBundleName())
                .withAbilityName(ABILITY_NAME)
                .withAction(abilitySliceName)
                .build();
        intent.setOperation(operation);
        startAbility(intent);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
